package com.zz.ecommerce.user;

import com.sdeport.common.vo.OrganizationJobVO;
import com.sdeport.common.vo.OrganizationVO;
import com.sdeport.common.vo.UserVO;
import com.sdeport.security.shiro.SDUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * 用户登录controller
 *
 * @author
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/getDistinctSign", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String getDistinctSign() {
        UserInfo userVo = getUserInfo();
        if(null!=userVo&&StringUtils.isNotEmpty(userVo.getLogName())){
            Map reMap=userDao.getDistinctSign(userVo.getLogName());
            return StringUtils.stripToEmpty(reMap.get("DISTINCT_SIGN").toString());
        }
        return "";
    }
    /**
     * 从session中获取用户数据
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/getSessionUser", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public UserInfo getSessionUser(HttpSession session) {
        UserInfo userVo = getUserInfo();
        session.setAttribute("UserInfo", userVo);
        return userVo;
    }

    private UserInfo getUserInfo() {
        UserInfo userVo = new UserInfo();
        //4px提供的jar包，从session中取到用户对象
        try {
            UserVO currentUser = SDUtils.getCurrentUser();
            String username = currentUser.getUsername();
            String departmentId = currentUser.getDepartmentId();
            String companyId = currentUser.getCompanyId();
            String companyName = currentUser.getCompanyName();
            String cnName = currentUser.getCnName();

            if (StringUtils.isNotEmpty(username)) {
                userVo.setLogName(username);
                userVo.setName(username);
            }
            if (StringUtils.isNotEmpty(departmentId)) {
                userVo.setDepartmentId(departmentId);
            }

            List<OrganizationJobVO> jobList = currentUser.getOrganizationJobs();
            if (!CollectionUtils.isEmpty(jobList)) {
                OrganizationVO organization = jobList.get(0).getOrganization();
                if (null != organization) {
                    userVo.setOrganizationId(organization.getOrganizationCode());
                }
            }

            if (StringUtils.isNotEmpty(companyId)) {
                userVo.setCompanyId(companyId);
            }
            if (StringUtils.isNotEmpty(companyName)) {
                userVo.setCompany(companyName);
            }
            if (StringUtils.isNotEmpty(cnName)) {
                userVo.setName(cnName);
            }
        } catch (Exception e) {
            logger.error("", e);
            userVo.setCompany("山东海运船舶管理有限公司");
            userVo.setCompanyId("1818");
            userVo.setCompanyName("山东海运船舶管理有限公司");
            userVo.setDepartmentId("00001");
            userVo.setName("lilin");
            userVo.setLogName("lilin");
            userVo.setOrganizationId("98");
        }

        return userVo;
    }

    @RequestMapping(value = "/editPasswd", method = {RequestMethod.GET, RequestMethod.POST})
    public String editPasswd() {
        return "user/changePassword";
    }

    @RequestMapping(value = "/doChangePassword", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public boolean doChangePassword(HttpSession session, String pswOldPsw, String pswNewPsw1, Model model) {
        logger.info("pswOldPsw=" + pswOldPsw);
        logger.info("pswNewPsw1=" + pswNewPsw1);
        UserInfo userInfo = (UserInfo) session.getAttribute("UserInfo");
        if (userInfo != null) {
            logger.info("getLogName=" + userInfo.getLogName());
            try {
                boolean success = SDUtils.updatePassword(userInfo.getLogName(), pswOldPsw,
                                                         pswNewPsw1);
                return success;
            } catch (Exception e) {
                logger.error("", e);
                return false;
            }
        } else {
            return false;
        }
    }

    @RequestMapping(value = "/rememberICLoginInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String rememberICLoginInfo(HttpServletRequest request) throws UnsupportedEncodingException {
        String loginResult = request.getParameter("loginResult");
        if (StringUtils.isNotEmpty(loginResult)) {
            request.getSession().setAttribute("ICLoginResult", loginResult);
            UserInfo userVo = getUserInfo();
            request.getSession().setAttribute("UserInfo", userVo);
        }
        return "ok";
    }

    @RequestMapping(value = "/getIcLoginInfo", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String getIcLoginInfo(HttpServletRequest request) throws UnsupportedEncodingException {
        String icLoginResult = (String) request.getSession().getAttribute("ICLoginResult");
        return StringUtils.isEmpty(icLoginResult) ? "" : icLoginResult;
    }
}
